package com._520it.crm.service.impl;

import com._520it.crm.domain.DiclearInfo;
import com._520it.crm.domain.Employee;
import com._520it.crm.domain.InsuranceInfoChange;
import com._520it.crm.domain.OrderBill;
import com._520it.crm.mapper.AuditInfoMapper;
import com._520it.crm.mapper.AuditInsuranceMapper;
import com._520it.crm.mapper.InsuranceInfoChangeMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.OrderBillQueryObject;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IOrderBillService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by 呆 on 2017/10/3.
 */
@Service
public class OrderBillServiceImpl implements IOrderBillService {
    @Autowired
    private OrderBillMapper orderBillMapper;

    @Autowired
    private AuditInfoMapper auditInfoMapper;
    @Autowired
    private AuditInsuranceMapper auditInsuranceMapper;
    @Autowired
    private InsuranceInfoChangeMapper insuranceInfoChangeMapper;



    @Override
    public int deleteByPrimaryKey(Long id) {
        return orderBillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(OrderBill orderBill) {
        //新增保单是设置保单的状态为 暂存单
        orderBill.setState(OrderBill.STATE_ZC);
        orderBillMapper.insert(orderBill);
        orderBillMapper.insertRelationsToPro(orderBill.getId(),orderBill.getProductId());
        return orderBill.getId();
    }

    @Override
    public OrderBill selectByPrimaryKey(Long id) {
        return orderBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderBill> selectAll() {
        return orderBillMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(OrderBill orderBill) {
        return orderBillMapper.updateByPrimaryKey(orderBill);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = orderBillMapper.queryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<OrderBill> data = orderBillMapper.queryforList(qo);
        return new PageResult(totalCount,data);
    }

    @Override
    public PageResult approvalQueryPage(OrderBillQueryObject queryObject) {
        try {
            queryObject.setState(OrderBill.STATE_SQPG);
            Integer totalCount = orderBillMapper.approvalQueryforCount(queryObject);
            if (totalCount==0){
                return new PageResult(0, Collections.EMPTY_LIST);
            }
            List<OrderBill> data = orderBillMapper.approvalQueryforList(queryObject);
            return new PageResult(totalCount,data);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public void updateTimeandBaseInfo(OrderBill orderBill) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        orderBill.setInputUserSn(employee.getSn());
        orderBill.setIssue(1);
        orderBillMapper.updateTimeandBaseInfo(orderBill);
    }

    @Override
    public void applyCheck(String insurancePolicyNumber) {
        orderBillMapper.updateStateByinsurancePolicyNumber(insurancePolicyNumber,OrderBill.STATE_SQHB);
    }
    @Override
    public void checkApply(Long orderId ,String remark) {
        OrderBill orderBill = orderBillMapper.selectByPrimaryKey(orderId);
        InsuranceInfoChange insuranceInfoChange = new InsuranceInfoChange();
        insuranceInfoChange.setOrderBillId(orderId);
        insuranceInfoChange.setPetitioner(orderBill.getClient().getId());
        insuranceInfoChange.setQueryNumber(orderBill.getClientCarInfo().getQueryNumber());
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        insuranceInfoChange.setEmployeeSn(employee.getSn());
        insuranceInfoChangeMapper.insertbyorderbill(insuranceInfoChange);
        orderBillMapper.checkApply(orderId ,remark,new Date());
    }

    @Override
    public BigDecimal querySpand(String insurancePolicyNumber) {
        return orderBillMapper.querySpand(insurancePolicyNumber);
    }

    @Override
    public OrderBill queryInsuranceReturn() {

        return orderBillMapper.queryInsuranceReturn(20);
    }

    @Override
    public PageResult issuedReturnQueryPage(OrderBillQueryObject queryObject) {
        queryObject.setState(OrderBill.STATE_ZC);
        Integer totalCount = orderBillMapper.approvalQueryforCount(queryObject);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<OrderBill> data = orderBillMapper.approvalQueryforList(queryObject);
        return new PageResult(totalCount,data);
    }

    @Override
    public PageResult queryByApplystate(OrderBillQueryObject qo) {
        try {
            qo.setState(10);
            Integer totalCount = orderBillMapper.approvalQueryforCount(qo);
            if (totalCount==0){
                return new PageResult(0, Collections.EMPTY_LIST);
            }
            List<OrderBill> data = orderBillMapper.approvalQueryforList(qo);
            return new PageResult(totalCount,data);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    //退回保存备注,并删除审核表关联
    @Override
    public void updateRemark(OrderBill bill) {
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();
        bill.setExamineUserSn(current.getSn());
        //更新状态和保存备注
        orderBillMapper.updateRemark(bill);

        //关联在审核表中的那条数据也要删掉
        //删除和待审核表中的关系
        auditInfoMapper.deleteAuditInsuranceRelation(bill.getId());
        //删除和批改表中的关系
        auditInsuranceMapper.deleteInsuranceInfoChangeRelation(bill.getId());

    }

    //修改状态
    @Override
    public void updateState(OrderBill bill) {

        orderBillMapper.updateState(bill);

    }

    @Override
    public PageResult approvalQueryPageReturn(OrderBillQueryObject queryObject) {
        queryObject.setState(OrderBill.STATE_HBNO);
        Integer totalCount = orderBillMapper.approvalQueryforCount(queryObject);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<OrderBill> data = orderBillMapper.approvalQueryforList(queryObject);
        return new PageResult(totalCount,data);
    }

    @Override
    public PageResult approvalQueryPageInZC(OrderBillQueryObject queryObject) {
        queryObject.setState(OrderBill.STATE_ZC);
        Integer totalCount = orderBillMapper.approvalQueryforCount(queryObject);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<OrderBill> data = orderBillMapper.approvalQueryforList(queryObject);
        return new PageResult(totalCount,data);
    }

    @Override
    public PageResult approvalQueryPageInApplyCheck(OrderBillQueryObject queryObject) {

        queryObject.setState(OrderBill.STATE_SQHB);
        Integer totalCount = orderBillMapper.approvalQueryforCount(queryObject);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<OrderBill> data = orderBillMapper.approvalQueryforList(queryObject);
        return new PageResult(totalCount,data);
    }



    //根据ID查询对应的明细:用于审核成功页面弹框
    @Override
    public OrderBill queryDetailData(Long id) {
        return orderBillMapper.queryDetailData(id);
    }

    @Override
    public DiclearInfo selectDiclearInfo(Long id) {
        return orderBillMapper.selectDiclearInfo(id);
    }
}
